package net.cz.wc.command;

import java.sql.Connection;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import net.cz.wc.dao.SQLDao;
import net.cz.wc.data.ConnectionManager;
import net.cz.wc.main.Context;
import net.cz.wc.olddto.IntervalDTO;
import net.cz.wc.output.ConsoleOutput;
import net.cz.wc.util.DateInterval;
import net.cz.wc.util.UtilDate;

public class IntervalCommand implements Command {

	private String interval = null;
	
	private String metricName = null;
	
	private String roundedToken = null;
	
	private String metricExclude = null;
	
	public String getInterval() {
		return interval;
	}
	
	private boolean validArgs(String[] args) {
		
		for (int i = 1; i < args.length; i++) {
			if ("-i".equals(args[i])) {
				interval = args[i + 1];
			}
			
			if ("-m".equals(args[i])) {
				metricName = args[i + 1];
			}
			
			if ("-exc".equals(args[i])) {
				metricExclude = args[i + 1];
			}
		}
		
		if (interval == null) {
			return false;
		}
		
		if ("dayly".equals(interval)) {
			interval = "24h";
			roundedToken = "day";
		}
		
		if (Suffix.getSuffix(interval.substring(interval.length() - 1, interval.length())) == null) {
			return false;
		}
		
		return true;
	}
	
	public void execute(String[] args) {
		if (!validArgs(args)) {
			Context.getContext().addErrorMessage("invalid args for ext command");
			return;
		}
		
		long totalSeconds = intervalToMiliseconds(getInterval());
		
		System.out.println("Total seconds per interval: " + totalSeconds);
		
		ConsoleOutput.printIntervalHeader();
		
		Connection conn = null;
		try {
			conn = ConnectionManager.getConnection();
			SQLDao dao = new SQLDao(conn);
		
			List<Date> dateList = dao.getMinAndMaxDate();
			Date minDate = dateList.get(0);
			Date maxDate = dateList.get(1);
			
			if ("day".equals(roundedToken)) {
				Calendar minCal = Calendar.getInstance();
				minCal.setTime(minDate);
				minCal.set(Calendar.HOUR_OF_DAY, 0);
				minCal.set(Calendar.MINUTE, 0);
				minCal.set(Calendar.SECOND, 0);
				minDate = minCal.getTime();
			}
			
			List<DateInterval> intervalList = UtilDate.createIntervals(minDate, maxDate, totalSeconds);
			
			for (DateInterval inter : intervalList) {
				IntervalDTO interDto = dao.getInterval(inter.getIntervalStart(), inter.getIntervalEnd(), metricName, metricExclude);
				
				ConsoleOutput.printInterval(interDto);
			}
		} catch (Exception ee) {
			Context.getContext().printException(ee);
		} finally {
			ConnectionManager.closeConnection(conn);
		}
	}
	
	
	private int intervalToMiliseconds(String interval) {
		
		String suffText = interval.substring(interval.length() - 1, interval.length());
		
		Suffix suff = Suffix.getSuffix(suffText);
		
		int totalSeconds = 
			suff.getMultiplier() * Integer.parseInt(interval.substring(0, interval.length() - 1));
		
		return totalSeconds;
	}
	
	enum Suffix {
		second ("s", 1),
		minute ("m", 60),
		hour ("h", 60*60),
		day ("d", 24*60*60);
		
		private String suffixText;
		private int multiplier;
		
		Suffix(String text, int mult) {
			this.suffixText = text;
			this.multiplier = mult;
		}

		public String getSuffixText() {
			return suffixText;
		}

		public int getMultiplier() {
			return multiplier;
		}
		
		public static Suffix getSuffix(String sfxText) {
			for (Suffix sf : Suffix.values()) {
				if (sfxText.equals(sf.getSuffixText())) {
					return sf;
				}
			}
			
			return null;
		}
	}
}
